<template>
    <div class="login">
      <!-- 登录页的导航 -->
      <van-nav-bar
            title="标题"
            left-text="返回"
            right-text="按钮"
            left-arrow
            @click-left="onClickLeft"
            @click-right="onClickRight"
        />
        <div class="container">
      <div class="title">
        <h3>手机号登录</h3>
        <p>未注册的手机号登录后将自动注册</p>
      </div>

      <div class="form">
        <div class="form-item">
          <input class="inp" maxlength="11" placeholder="请输入手机号码" type="text" v-model="mobile">
        </div>
        <div class="form-item">
          <input class="inp" maxlength="5" placeholder="请输入图形验证码" type="text" v-model="picCode">
          <img :src="picImg" alt="" @click="getPicCode">
        </div>
        <div class="form-item">
          <input class="inp" placeholder="请输入短信验证码" type="text" v-model="smsCode">
          <button ref="ipt" @click="checkPiceCode">获取验证码</button>
        </div>
      </div>

      <div class="login-btn" @click="login">登录</div>
    </div>
    </div>
</template>

<script>

import { Toast } from 'vant'
import { getPicCode, checkPicCode, login } from '@/api/login'
import { setInfo } from '@/utils/storage'
export default {
  name: 'LoginPapge',
  data () {
    return {
      picCode: '',
      picImg: '',
      picKey: '',
      Picmd5: '',
      mobile: '',
      smsCode: '',
      time: 5,
      timer: '',
      userId: '',
      token: ''
    }
  },
  methods: {
    onClickLeft () {
      Toast('返回')
    },
    onClickRight () {
      Toast('按钮')
    },
    async getPicCode () {
      const { data: { base64, key, md5 } } = await getPicCode()
      this.picImg = base64
      this.picKey = key
      this.Picmd5 = md5
    },
    async checkPiceCode () {
      // 1.倒计时
      await checkPicCode(this.picKey, this.Picmd5, this.mobile)
      if (this.validFn()) {
        this.timer = setInterval(() => {
          this.$refs.ipt.innerHTML = `剩余时间${this.time}`
          this.$refs.ipt.disabled = true

          if (this.time === -1) {
            clearInterval(this.timer)
            this.$refs.ipt.innerHTML = '重新获取'
            this.time = 5
            this.$refs.ipt.disabled = false
            return
          }

          this.time = this.time - 1
        }, 1000)
        this.$toast('发送成功，请注意查收')
      }

      // 2.封装一个图形验证码验证
    },
    validFn () {
      if (!/^1[3-9]\d{9}$/.test(this.mobile)) {
        this.$toast('请输入正确的手机号')
        return false
      }
      if (!/^\w{4}$/.test(this.picCode)) {
        this.$toast('请输入正确的图形验证码')
        return false
      }
      return true
    },
    async login () {
      if (this.validFn()) {
        const res = await login(this.mobile, this.smsCode)

        if (res.status === 200) {
          this.$store.commit('user/getToken', {
            token: res.data.token,
            userId: res.data.userId
          })
        }
        setInfo({
          token: res.data.token,
          userId: res.data.userId
        })

        this.$router.push({
          path: '/cart'
        })
      }
    }

  },
  created () {
    this.getPicCode()
  }
}
</script>

<style lang="less" scoped>
.container {
  padding: 49px 29px;

  .title {
    margin-bottom: 20px;
    h3 {
      font-size: 26px;
      font-weight: normal;
    }
    p {
      line-height: 40px;
      font-size: 14px;
      color: #b8b8b8;
    }
  }

  .form-item {
    border-bottom: 1px solid #f3f1f2;
    padding: 8px;
    margin-bottom: 14px;
    display: flex;
    align-items: center;
    .inp {
      display: block;
      border: none;
      outline: none;
      height: 32px;
      font-size: 14px;
      flex: 1;
    }
    img {
      width: 94px;
      height: 31px;
    }
    button {
      height: 31px;
      border: none;
      font-size: 13px;
      color: #cea26a;
      background-color: transparent;
      padding-right: 9px;
    }
  }

  .login-btn {
    width: 100%;
    height: 42px;
    margin-top: 39px;
    background: linear-gradient(90deg,#ecb53c,#ff9211);
    color: #fff;
    border-radius: 39px;
    box-shadow: 0 10px 20px 0 rgba(0,0,0,.1);
    letter-spacing: 2px;
    display: flex;
    justify-content: center;
    align-items: center;
  }
}
</style>
